<!--
 * @Descripttion: 开票
 * @version:
 * @Author: 张文韬
 * @Date: 2020-10-13 11:49:35
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-26 18:03:04
-->
<template>
  <el-card class="box-card">
    <div class="control-box">
      <!-- search 搜索区 START -->
      <e-search ref="searchFormRef" :form-options="formOptions"
                :form-label-width="searchLabelWidth"
                :refresh-show="true"
                @on-search="searchControlHandle"
                @on-refresh="refreshControlHandle"
                @change-height="changeControlHeight" />
      <!-- search 搜索区 END -->
    </div>
    <!-- 功能按钮区 START -->
    <el-row class="options-Area" type="flex" justify="end">
      <el-button @click="handleDownloadOrderInfo()" type="primary" size="small">下载订单信息</el-button>
      <el-upload
          class=""
          :headers="fileConfig.headers()"
          :action="uploadUrl"
          multiple
          :limit="1"
          :file-list="fileList"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          accept=".xlsx, .xls"
          :before-upload="beforeUpload"
          :on-exceed="handleExceed">
        <el-button class="upload-btn" type="primary" size="small">上传发票</el-button>
      </el-upload>
    </el-row>
    <div class="table-box">
      <!-- table 数据表格 START -->
      <e-table :data="tableData"
               :columns="columns"
               stripe
               border
               :max-height="tableMaxHeight"
               style="width: 100%"
               @selection-change="changeTableSelectData"
               :current="pageData.pageNum"
               :total="pageData.total"
               @page-size-change="changePageSize"
               @page-current-change="changePageCurrent">
      </e-table>
      <!-- table 数据表格 END -->
    </div>
  </el-card>
</template>

<script>
import eSearch from '@/components/e-search'
import { formOptions, btns } from './control'
import columns from './columns'
import { queryInvoiceList, querySuppliers, exportInvoiceInfo } from '@/api/admin.api'
// 上传文件
import fileUpdata from '@/api/file.config'
import { beforeUpload } from '@/util/common'
// eslint-disable-next-line
const baseUrl = process.env.VUE_APP_BASE_API

export default {
  name: 'invoice',
  components: {
    eSearch
  },
  props: {},
  data () {
    return {
      formOptions,
      btns,
      columns,
      searchLabelWidth: '100px',
      editFormLabelWidth: '100px',

      // 编辑框是否展示
      editDialogShow: false,
      // 编辑框表单数据
      dialogFormCurrent: {},
      // 查询表单
      searchFormData: {},
      // 控制区高度
      controlHeight: 0,
      // 表格数据
      tableData: [],
      // 分页数据
      pageData: {
        // 当前分页
        pageNum: 1,
        // 分页条数
        pageSize: 10,
        // 数据总数
        total: 0
      },
      // 表格选中数据
      tableSelectData: [],
      // 附件列表
      fileList: [],
      fileConfig: fileUpdata,
      uploadUrl: `${baseUrl}/api/invoice/uploadInvoice`
    }
  },
  watch: {},
  computed: {
    // 组合查询条件和分页
    searchData () {
      const { pageNum, pageSize } = this.pageData
      return { ...this.searchFormData, size: pageSize, current: pageNum }
      // return Object.assign(this.searchFormData,{size: pageSize},{current:pageNum})
    },
    // 表单实例
    dialogForm () {
      return this.$refs.dialogForm
    },
    //表格最大高度
    tableMaxHeight () {
      return window.innerHeight - this.controlHeight - 260
    }
  },
  created () {
    this.init()
  },
  methods: {
    /**
     * @functionName init
     * @description 页面初始化
     * @author 张航
     * @date 2020-09-09 08:40:06
     * @version V1.0.0
     */
    init () {
      this.getTableData()
      this.getLabelData()
    },
    // 获取菜单弹出项
    getLabelData () {
      // 获取电商筛选类型
      querySuppliers().then(res => {
        // console.log(res)
        res.map((item, index) => {
          const optionObj = {}
          optionObj.value = item.id
          optionObj.label = item.orgNameZhShort
          this.$set(this.formOptions[0].options, index, optionObj)
        })
      })
    },
    /**
     * @functionName getTableData
     * @description 获取表格数据
     * @author 张航
     * @date 2020-10-27 09:20:44
     * @version V1.0.0
     */
    getTableData () {
      // console.log(this.searchData)
      queryInvoiceList(this.searchData).then(res => {
        this.tableData = res.records
        this.pageData.total = res.total
      })
    },
    /**
     * @functionName searchControlHandle
     * @param {Object} data 查询参数
     * @description 查询
     * @author 张航
     * @date 2020-09-08 15:01:26
     * @version V1.0.0
     */
    searchControlHandle (data) {
      this.searchFormData = data
      // console.log(data)
      this.refreshPageCurrent()
    },
    /**
     * @functionName refreshControlHandle
     * @param {Object} data 重置后的数据
     * @description 重置查询条件
     * @author 张航
     * @date 2020-09-08 17:01:51
     * @version V1.0.0
     */
    refreshControlHandle (data) {
      // console.log(data)
      this.searchFormData = data
    },
    /**
     * @functionName changeControlHeight
     * @param {String} height 控制区占用高度
     * @description 控制区高度变化
     * @author 张航
     * @date 2020-09-16 16:17:07
     * @version V1.0.0
     */
    changeControlHeight (height) {
      this.controlHeight = height
    },
    /**
     * @functionName changeTableSelectData
     * @param {Array} rows 选中数据
     * @description 表格选中数据改变
     * @author 张航
     * @date 2020-09-08 17:52:12
     * @version V1.0.0
     */
    changeTableSelectData (rows) {
      this.tableSelectData = rows
    },
    /**
     * @functionName checkTableSelectData
     * @return {Number} 返回结果
     * @description 方法描述
     * @author 张航
     * @date 2020-09-10 09:58:59
     * @version V1.0.0
     */
    checkTableSelectData () {
      if (this.tableSelectData.length === 0) {
        this.$alert('至少选择一条数据！', '提示', {
          confirmButtonText: '确定'
        })
      }
      return this.tableSelectData.length
    },
    /**
     * @functionName changePageSize
     * @param {Number} size 分页条数
     * @description 修改分页条数
     * @author 张航
     * @date 2020-09-08 17:57:06
     * @version V1.0.0
     */
    changePageSize (size) {
      this.pageData.pageSize = size
      this.getTableData()
    },
    /**
     * @functionName changePageCurrent
     * @param {Number} current 当前分页
     * @description 修改当前分页
     * @author 张航
     * @date 2020-09-08 18:02:03
     * @version V1.0.0
     */
    changePageCurrent (current) {
      this.pageData.pageNum = current
      this.getTableData()
    },
    /**
     * @functionName refreshPageCurrent
     * @description 重置当前分页
     * @author 张航
     * @date 2020-09-22 10:45:57
     * @version V1.0.0
     */
    refreshPageCurrent () {
      this.pageData.pageNum = 1
      this.getTableData()
    },

    // 提交检测判断
    handleExceed(files, fileList) {
      this.$message.warning(`最多可同时上传 1 个附件，您已超限`)
    },
    beforeUpload(file) {
      const isLt500M = file.size / 1024 / 1024 < 500
      if (!isLt500M) {
        this.$message.error('上传附件大小不能超过 500MB!')
      }
      return isLt500M
    },
    // 文件上传成功
    handleUploadSuccess(res, file) {
      if (res.data==='true') {
        this.$message.success('上传成功！')
        this.refreshPageCurrent()
      }else{
        this.$message.warning(res.msg||'上传失败！')
      }
      this.fileList = []
    },
    // 文件上传失败
    handleUploadError(res, file) {
      let result = JSON.parse(res.message)
      this.$message.warning(result.msg||'上传失败！')
    },
    /**
     * @functionName saveAs
     * @description 保存下载的文件
     * @author 张文韬
     * @date 2020-10-27 09:20:32
     * @version V1.0.0
     */
    saveAs(blob, filename) {
      if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, filename);
      }
      else {
        var link = document.createElement("a");
        var body = document.querySelector("body");
        link.href = window.URL.createObjectURL(blob);
        link.download = filename;
        link.style.display = "none";
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
      }
    },
    /**
     * @functionName handleDownloadOrderInfo
     * @description 处理下载订单信息事件
     * @author 张文韬
     * @date 2020-10-27 09:20:17
     * @version V1.0.0
     */
    handleDownloadOrderInfo () {
      exportInvoiceInfo(this.searchFormData).then((res) => {
        let blob = new Blob([res], { type: "application/vnd.ms-excel" });
        this.saveAs(blob, "订单信息.xlsx");
      });
    }
  },
  mounted () { }
}
</script>
<style lang="scss">
  .box-card {
    margin: 4px;
    height: calc(100vh - 110px);
    .el-card__body {
      padding: 0;
    }
    .upload-btn {
      margin: 0 10px;
    }
  }
  .abc-header {
    color: #0000ff;
  }

  .control-box {
    margin-bottom: 10px;
  }
  .options-Area{
    margin-bottom: 10px;
  }
</style>
